﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace blackhouse.WebNemo.Business.Groups {
    public abstract class RecordExtension {

        #region Fields

        private GroupRecord record;

        #endregion

        #region Properties

        #endregion

        #region Constructors

        public RecordExtension(GroupRecord record) {
            this.record = record;
        }

        #endregion

        #region Methods

        public string GetIdentyficator() {
            return this.GetIdentyficator(this.record);
        }

        protected abstract string GetIdentyficator(GroupRecord record);

        public void RecordChanged(RecordChangeType type) {
            this.RecordChanged(record, type);
        }

        protected abstract void RecordChanged(GroupRecord record, RecordChangeType type);

        protected void SaveRecord() {
            this.record.Save(false);
        }

        public void RecordTranslated(CultureInfo language, int columnId, string prevValue, string value) {
            this.RecordTranslated(language, columnId, (this.record != null) ? this.record.RecordId : 0, prevValue, value);
        }

        public virtual void RecordTranslated(CultureInfo language, int columnId, int recordId, string prevValue, string value) {
        }

        protected abstract void RecordTranslated(GroupRecord record, GroupColumn column, string prevValue, string value);

        public bool RecordIsValid() {
            string v = String.Empty;
            return this.RecordIsValid(out v);
        }

        public virtual bool RecordIsValid(out string notValidMessage) {
            notValidMessage = "";
            return true;
        }

        public virtual bool ShowOnRecordsList(GroupRecord record) {
            return true;
        }

        public virtual bool ShowOnReferencesRecordsList(GroupRecord record, GroupColumn parentColumn) {
            return true;
        }

        /// <summary>
        /// Czy na liście edycji rekordu (rekord z grupy dla której jest rozszerzenie) ma zostać wyświetlony
        /// </summary>
        /// <param name="referenceRecord">Relacyjny rekord</param>
        /// <param name="parentColumn">kolumna z tej grupy o którą się rozchodzi</param>
        /// <returns></returns>
        public virtual bool ShowRefferRecordOnReferencesList(GroupRecord referenceRecord, GroupColumn parentColumn) {
            return true;
        }

        public static RecordExtension GetRecordExtension(IGroup group) {
            return GetRecordExtension(group, null);
        }

        public static RecordExtension GetRecordExtension(IGroup group, GroupRecord record) {
            string extensionName = group.GetRecordExtensionName();
            if (string.IsNullOrEmpty(extensionName))
                return RecordExtension.NULL;

            Type t = Type.GetType(extensionName, false, false);
            if (t == null)
                return RecordExtension.NULL;

            RecordExtension ext = Activator.CreateInstance(t, record) as RecordExtension;
            if (ext == null)
                return RecordExtension.NULL;

            return ext;
        }

        #endregion

        #region NULL

        private static RecordExtension nulle = new NULLRecordExtension();

        public static RecordExtension NULL {
            get { return nulle; }
        }

        private class NULLRecordExtension : RecordExtension {

            public NULLRecordExtension() : base(null) { }

            protected override string GetIdentyficator(GroupRecord record) {
                return "";
            }

            protected override void RecordChanged(GroupRecord record, RecordChangeType type) { }

            protected override void RecordTranslated(GroupRecord record, GroupColumn column, string prevValue, string value) { }

        }

        #endregion

    }

}
